package com.shen.ball;

import com.shen.util.RandomNumber;

import java.io.*;
import java.util.*;

/**
 * Author:shen
 * CreateDate:2020/6/7.
 * Function:
 */
public class Ball implements Serializable{
   private List<Integer> redBall;
   private Integer blueBall;

    public Ball() {
    }

    public Ball(List<Integer> redBall, Integer blueBall) {
        this.redBall = redBall;
        this.blueBall = blueBall;
    }

    public List<Integer> getRedBall() {
        return redBall;
    }

    public void setRedBall(List<Integer> redBall) {
        this.redBall = redBall;
    }

    public Integer getBlueBall() {
        return blueBall;
    }

    public void setBlueBall(Integer blueBall) {
        this.blueBall = blueBall;
    }

    @Override
    public String toString() {
        return "Ball{" +
                "redBall=" + redBall +
                ", blueBall=" + blueBall +
                '}';
    }
}


 class Lottery{
     //随机生成
     static RandomNumber randomNumber=new RandomNumber();
     /**
      * 生成一条ball信息
      */
     public static void init(){
         //生成的中奖号码
        Ball ball=new Ball();

         List<Integer> redBall = randomNumber.randomNumber(33, 1, 6);
         Collections.sort(redBall);//排序
         List<Integer> blueBall = randomNumber.randomNumber(16, 1, 1);

         ball.setRedBall(redBall);
         ball.setBlueBall(blueBall.get(0));
         serializationBall(ball);
     }

     /**
      * 序列化球储存生成的奖
      */
     public static  void serializationBall(Ball ball){
         ObjectOutput objectOutput=null;
         try {
             objectOutput=new ObjectOutputStream(new FileOutputStream("E:/a/a.txt"));
             objectOutput.writeObject(ball);
             objectOutput.flush();
         } catch (IOException e) {
             e.printStackTrace();
         }finally {
             if(objectOutput!=null){
                 try {
                     objectOutput.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     }

     /**
      *读取序列化文件
      */
     public static Ball serializationInBall(){
         Ball ball =null;
         ObjectInput objectInput=null;
         try {
             objectInput=new ObjectInputStream(new FileInputStream("E:/a/a.txt"));
              ball = (Ball) objectInput.readObject();
         } catch (IOException e) {
             e.printStackTrace();
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } finally {
             if(objectInput!=null){
                 try {
                     objectInput.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
         return ball;
     }

     /**
      * 对比红色球是否相同
      */
     public static int checkRadBall(Ball userBall,Ball systemBall){
        int count=0;
         List<Integer> userBallRedBall = userBall.getRedBall();
         List<Integer> systemBallRedBall = systemBall.getRedBall();
         for (Integer ball: userBallRedBall) {
             if(systemBallRedBall.contains(ball)){
                 count++;
             }
         }
         return count;
     }
     /**
      * 对比用户是否中奖
      */
     public static int isLottery(Ball userBall){
         int flag=0;//标记几等奖
         if(userBall!=null){
             Ball ball = serializationInBall();
             System.out.println(ball.toString());
             System.out.println(userBall.toString());
             int count = checkRadBall(userBall, ball);
             if(ball.getBlueBall().equals(userBall.getBlueBall())){
                 flag=count==0 ? flag=6:flag;
                 flag=count==3 ? flag=5:flag;
                 flag=count==4 ? flag=4:flag;
                 flag=count==5 ? flag=3:flag;
                 flag=count==6 ? flag=1:flag;
             }else{
                 flag=count==6 ? flag=2:flag;
             }
             System.out.println(count);
         }
         return flag;
     }


     static Scanner scanner=new Scanner(System.in);
     public static void main(String[] args) {
        // init();//初始化球

        Ball ball=new Ball();
        List<Integer> lists=new ArrayList<>();
        for (int i=0;i<6;i++){
            System.out.println("请输入第"+(i+1)+"位红色球号");
            int num=scanner.nextInt();
            if(lists.contains(num)){
                System.out.println("该数字重复请重新输入！");
                i--;
            }else{
                lists.add(num);
            }
        }
        ball.setRedBall(lists);
         System.out.println("请输入蓝色球号：");
         Integer num=scanner.nextInt();
         ball.setBlueBall(num);
         int lottery = isLottery(ball);
         switch (lottery){
             case 1:
                 System.out.println("恭喜获得一等奖");
                 break;
             case 2:
                 System.out.println("恭喜获得二等奖");
                 break;
             case 3:
                 System.out.println("恭喜获得三等奖");
                 break;
             case 4:
                 System.out.println("恭喜获得四等奖");
                 break;
             case 5:
                 System.out.println("恭喜获得五等奖");
                 break;
             case 6:
                 System.out.println("恭喜获得六等奖");
                 break;
                 default:
                     System.out.println("没有得奖!");
         }


     }
 }
